package top.lingchen.webfluxdemo2;

import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import top.lingchen.webfluxdemo2.entity.User;

/**
 * @Author 凌宸
 * @create 2021-12-01 下午 8:49
 * @Description
 * @Version 1.0
 */
public class Client {
    public static void main(String[] args) {
        // 调用服务器地址
        WebClient webClient = WebClient.create("http://localhost:54636");
        // 根据 id 查询
        String id = "1";
        User user = webClient.get().uri("/users/{id}", id)
                .accept(MediaType.APPLICATION_JSON)
                .retrieve().bodyToMono(User.class).block();
        System.out.println(user); // User{name='露西', gender='女', age=20}

        // 查询所有
        Flux<User> results = webClient.get().uri("/users")
                .accept(MediaType.APPLICATION_JSON).retrieve().bodyToFlux(User.class);
        results.map(s -> s.toString()).buffer()
                .doOnNext(System.out::println).blockFirst();
        // [User{name='露西', gender='女', age=20}, User{name='杰克', gender='男', age=25}, User{name='库里', gender='男', age=30}]
    }
}
